<?php

namespace app\api\controller;

use tool\Price;

class Pt extends Base
{

    public function opengroup()
    {
       $user_id=$this->getuser();
       $pt_activity_id=i("pt_activity_id","请选择活动");
       $pt_activity=model("ptActivity")->where("id",$pt_activity_id)->find();
       if(empty($pt_activity)){
           e("活动不存在");
       }
       $group=[];
       $group['pt_activity_id']=$pt_activity_id;
       $group['user_id']=$user_id;
       $group['timestamp']=time();
       $group['datetime']=date("Y-m-d H:i:s", $group['timestamp']);
        $group['end_timestamp']=$group['timestamp']+$pt_activity['hours']*3600;
        $group['end_datetime']=date("Y-m-d H:i:s", $group['end_timestamp']);
        $group['number']=$pt_activity['number'];
        $group['discount']=$pt_activity['discount'];
        model("ptGroup")->save($group);

       return s(model("ptGroup")->id);
    }

    public function getgroup(){
        $pt_group_id=i("pt_group_id");
        $group=model("ptGroup")->where("id",$pt_group_id)->find();
        if($group['cnumber']<$group['number']&& time()>$group['end_timestamp']&&$group['status']==0){
            model("ptGroup")->where("id",$pt_group_id)->update(['status'=>2]);
        }
        $pt_activity=model("ptActivity")->where("id",$group['pt_activity_id'])->find();
        $group['activity']=$pt_activity;
        $goods=model("goods")->where("id",$pt_activity['goods_id'])->find();
        $group['goods']=$goods;
        $group['userlist']=model("ptOrder")->where("pt_group_id",$pt_group_id)->join("user","pt_order.user_id=user.id","left")->select();
        return s($group);
    }
    public function activity_lists(){
        $lists=model("ptActivity")->where("isdelete",0)->order("sort asc")->select();
        for($i=0;$i<count($lists);$i++){
            $lists[$i]['goods']=model("goods")->where("id",$lists[$i]['goods_id'])->find();
            $lists[$i]['pt_price']=(intval($lists[$i]['goods']['price']*100)-intval($lists[$i]['discount']*100))/100;
        }
        return s($lists);
    }

}
